#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <sys/reg.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>
#include <signal.h>

void ex_program(int sig);

int main()
{
  (void) signal(SIGINT, ex_program);
  (void) signal(SIGHUP, ex_program);
  (void) signal(SIGQUIT, ex_program);
  (void) signal(SIGILL, ex_program);
  (void) signal(SIGTRAP, ex_program);
  (void) signal(SIGKILL, ex_program);
  (void) signal(SIGTERM, ex_program);
  pid_t pid;
  char * fifo = "/tmp/osd-wrapper";

  if (mkfifo(fifo, S_IRWXU) == -1)
    {
      perror("mkfifo failed!\n");
      unlink(fifo);
      exit(1);
    }

  pid = fork();

  if (pid == 0)
    {
      int fd = open(fifo, O_RDONLY);
      if (dup2(fd, 0) == -1)
	{
	  perror("Error: dup fifo failed.\n");
	  unlink(fifo);
	  exit(1);
	}
      close(fd);

	if (execl("/usr/bin/osd_cat", "osd_cat", "-p", "bottom", "-A", "right", "-c", "blue", "-o", "10", "-i", "50", "-f", "-*-lucidatypewriter-medium-*-*-*-20-*-*-*-*-*-*-*", "-d", "300", NULL) < 0) {
	//if (execl("/usr/bin/osd_cat", "osd_cat", NULL) < 0)
	  printf("Error: fork failed.\n");
	  unlink(fifo);
	}

    }
  else
    {
      //while (1)
      //	{
      	  int fd = open(fifo, O_WRONLY);
      	  FILE* in = fdopen(fd, "w");
      	  //FILE* out = fdopen(pipe_fdw[0], "r");

      	  //char line[4096];
      	  //fgets(line, 4096, stdin);
      	  //fprintf(in, "%s", line);
      	  /*flush buffer*/
      	  //fflush(in);
//	  sleep(1000);

     // 	}
      // Never reached.
      wait(NULL);
    }

  return 0;
}

void ex_program(int sig) {
  char * fifo = "/tmp/osd-wrapper";
  unlink(fifo);
  exit(1);
}
